/**  

 * @Title: DownLoadDocAction.java 

 * @Package tjwork.controller 

 * @Description: TODO(��һ�仰�������ļ���ʲô) 

 * @author A18ccms A18ccms_gmail_com  

 * @date 2012-12-18 ����07:17:58 

 * @version V1.0  

 */
package com.ddtou.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ddtou.ZipUtils;
import com.ddtou.entity.DataDownloadEntity;
import com.ddtou.entity.DataEntity;
import com.ddtou.service.DataEntityService;


/**
 * 
 * @version
 * 
 */
public class DownLoadDocAction extends HttpServlet {
    private static int DEFAULT_BUFFER_SIZE = 1024 * 4;
    private static Map<String,String> datatype = new HashMap<String,String>(){{
    	put("1","嫦娥一号CCD03级");
    	put("2","嫦娥二号CCD03");
    	put("3","嫦娥一号激光高度计");
    	put("4","LIOC");
    	put("5","LOLA");
    }};
    private DataEntityService dataEntityService;
    /**
     * Constructor of the object.
     */
    public DownLoadDocAction() {
        super();
        dataEntityService = new DataEntityService();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.service(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.service(request, response);
    }

    /**
     * Initialization of the servlet. <br>
     * 
     * @throws ServletException
     *             if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

    /*
     * (Javadoc)
     * 
     * <p>Title: service</p>
     * 
     * <p>Description: </p>
     * 
     * @param req
     * 
     * @param resp
     * 
     * @throws ServletException
     * 
     * @throws IOException
     * 
     * @see
     * javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest
     * , javax.servlet.http.HttpServletResponse)
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String userid = req.getParameter("userid");
        String type = req.getParameter("type");
        String name = req.getParameter("name");
        dataEntityService.userDownloadLog(new DataDownloadEntity(id, userid));
        ServletOutputStream output = null;
        FileInputStream input = null;
        String dlname = name+"-"+datatype.get(type)+".zip";
        File file = new File("D:/data/" + name + "/"+type+".zip");
        
        if (!file.exists()) {
        	try {
				ZipUtils.compress("d:\\data\\"+ name +"\\"+datatype.get(type)+".tiff");
				file = new File("D:/data/" + name + "/"+type+".zip");
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        try {
            resp.setHeader("Content-disposition", "attachment;filename=" + id + URLEncoder.encode("数据", "utf-8")+ dlname);

            resp.setContentType("application/zip");
            resp.setContentLength((int) file.length());
            output = resp.getOutputStream();
            input = new FileInputStream(file);
            copy(input, output);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (output != null)
                output.close();
            if (input != null)
                input.close();
        }

    }

    private static int copy(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int count = 0;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }

}
